package com.example.demo.controller;


import com.example.demo.base.*;
import com.example.demo.entity.Complain;
import com.example.demo.entity.House;
import com.example.demo.entity.Station;
import com.example.demo.entity.StationLog;
import com.example.demo.service.ComplainService;
import com.example.demo.service.HouseService;
import com.example.demo.service.StationLogService;
import com.example.demo.service.StationService;
import com.example.demo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Validated
@RequestMapping({"/manage"})
public class StationController {

    @Autowired
    private StationService stationService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private StationLogService stationLogService;

    @RequestMapping("/station/list")
    public BaseResponse list(
            @RequestParam(required = false, value = "userId") String userId,
            @RequestParam(required = false, value = "state") Integer state,
            @RequestParam(required = false, value = "carId") String carId,
            @RequestParam(required = false, value = "page") Integer pageNo,
            @RequestParam(required = false, value = "rows") Integer pageSize,
            HttpServletRequest request){
        QueryBuilder queryBuilder = new QueryBuilder();
        if(StringUtils.isNotBlank(userId)){
            queryBuilder.addFilter(Filter.eq("user_id",userId));

            House house = houseService.selectEntity(queryBuilder);
            if(house!=null){
                queryBuilder.clear();
                queryBuilder.addFilter(Filter.eq("floor_id",house.getFloorId()))
                            .addFilter(Filter.eq("unit_id",house.getUnitId()))
                            .addFilter(Filter.eq("room_id",house.getRoomId()));
            }else{
                return BaseResponse.success();
            }
        }
        if(state!=null){
            queryBuilder.addFilter(Filter.eq("state",state));
        }
        if(StringUtils.isNotBlank(carId)){
            queryBuilder.addFilter(Filter.eq("car_id",carId));
        }
        Pageable<Station> pageable = stationService.selectEntitiesByPage(queryBuilder,pageNo,pageSize);

        BaseResponse response = new BaseResponse();
        response.setRows(pageable.getRecords());
        response.setTotal(new Long(pageable.getTotal()).intValue());

        return response;
    }

    @RequestMapping("/station/add")
    public BaseResponse add(
            @RequestParam(required = false, value = "carId") String carId,
            HttpServletRequest request){

        Station station = new Station();
        station.setCarId(carId);
        stationService.insertEntity0(station);
        return BaseResponse.success();
    }

    @RequestMapping("/station/update")
    public BaseResponse update(
            @RequestParam(required = false, value = "carId") String carId,
            @RequestParam(required = false, value = "floorId") Integer floorId,
            @RequestParam(required = false, value = "unitId") Integer unitId,
            @RequestParam(required = false, value = "roomId") String roomId,
            HttpServletRequest request){

        Station station = new Station();
        station.setFloorId(floorId);
        station.setUnitId(unitId);
        station.setRoomId(roomId);
        station.setState(1);
        UpdateBuilder updateBuilder = new UpdateBuilder(station);
        updateBuilder.addFilter(Filter.eq("car_id",carId));
        stationService.updateEntity(updateBuilder);
        return BaseResponse.success();
    }

    @RequestMapping("/station/log")
    public BaseResponse log(
            @RequestParam(required = false, value = "carId") String carId,
            @RequestParam(required = false, value = "page") Integer pageNo,
            @RequestParam(required = false, value = "rows") Integer pageSize,
            HttpServletRequest request){
        QueryBuilder queryBuilder = new QueryBuilder();
        queryBuilder.addFilter(Filter.eq("car_id",carId))
                    .addOrder(Order.desc("id"));
        Pageable<StationLog> pageable = stationLogService.selectEntitiesByPage(queryBuilder,pageNo,pageSize);

        BaseResponse response = new BaseResponse();
        response.setRows(pageable.getRecords());
        response.setTotal(new Long(pageable.getTotal()).intValue());

        return  response;
    }

}
